package com.github.postsanf.yinian.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.SystemUtils;
import com.github.postsanf.yinian.utils.UIHelper;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;
import java.util.Calendar;
import java.util.TimeZone;

public class EveryDayNoticeDialogActivity extends Activity implements View.OnClickListener {
    private RelativeLayout dialog_mynotice;
    private RelativeLayout rl_mynotice;
    private ImageView everynotice_dayimg;
    private ImageView everynotice_daytext;
    private ImageView everynotice_daymic;
    private ImageView everynotice_daybtn;
    boolean isComplete;
    private MediaPlayer mMediaPlayer = null;
    private String Eaudio;
    int mWay = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_dialog_everyday_notice);
        Eaudio = "http://o9im0tspz.bkt.clouddn.com/yinian_push_audio.mp3";
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mWay = c.get(Calendar.DAY_OF_WEEK);
        registerBroadcastReceiver();
        initView();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    private void registerBroadcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("noticefinish");
        this.registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    // 广播接收
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "noticefinish":
                    finish();
                    break;
            }
        }
    };


    private void initView() {
        mMediaPlayer = new MediaPlayer();
        dialog_mynotice = (RelativeLayout) findViewById(R.id.dialog_mynotice);
        dialog_mynotice.setOnClickListener(this);
        findViewById(R.id.everynotice_dayclose).setOnClickListener(this);
        everynotice_dayimg = (ImageView) findViewById(R.id.everynotice_dayimg);
        everynotice_daytext = (ImageView) findViewById(R.id.everynotice_daytext);
        everynotice_daybtn = (ImageView) findViewById(R.id.everynotice_daybtn);
        everynotice_daymic = (ImageView) findViewById(R.id.everynotice_daymic);
        everynotice_daymic.setOnClickListener(this);
        everynotice_daybtn.setOnClickListener(this);
        rl_mynotice = (RelativeLayout) findViewById(R.id.rl_mynotice);
        Animation translateAnimationf = new TranslateAnimation(0, 0, -UIHelper.dip2px(EveryDayNoticeDialogActivity.this, 220), 0);
        translateAnimationf.setDuration(500);
        rl_mynotice.startAnimation(translateAnimationf);
        switch (mWay) {
            case 1:
            case 7:
                everynotice_dayimg.setImageResource(R.drawable.everynotice_dayimg6);
                everynotice_daytext.setImageResource(R.drawable.everynotice_daytext6);
                everynotice_daybtn.setImageResource(R.drawable.everynotice_daybtn6);
                everynotice_daymic.setVisibility(View.INVISIBLE);
                break;
            case 2:
                everynotice_dayimg.setImageResource(R.drawable.everynotice_dayimg1);
                everynotice_daytext.setImageResource(R.drawable.everynotice_daytext1);
                everynotice_daybtn.setImageResource(R.drawable.everynotice_daybtn1);
                everynotice_daymic.setVisibility(View.INVISIBLE);
                break;
            case 3:
                everynotice_dayimg.setImageResource(R.drawable.everynotice_dayimg2);
                everynotice_daytext.setImageResource(R.drawable.everynotice_daytext2);
                everynotice_daybtn.setImageResource(R.drawable.everynotice_daybtn2);
                everynotice_daymic.setVisibility(View.INVISIBLE);
                break;
            case 4:
                everynotice_dayimg.setImageResource(R.drawable.everynotice_dayimg3);
                everynotice_daytext.setImageResource(R.drawable.everynotice_daytext3);
                everynotice_daybtn.setImageResource(R.drawable.everynotice_daybtn3);
                everynotice_daymic.setVisibility(View.INVISIBLE);
                break;
            case 5:
                everynotice_dayimg.setImageResource(R.drawable.everynotice_dayimg4);
                everynotice_daytext.setImageResource(R.drawable.everynotice_daytext4);
                everynotice_daybtn.setImageResource(R.drawable.everynotice_daybtn4);
                everynotice_daymic.setVisibility(View.VISIBLE);
                break;
            case 6:
                everynotice_dayimg.setImageResource(R.drawable.everynotice_dayimg5);
                everynotice_daytext.setImageResource(R.drawable.everynotice_daytext5);
                everynotice_daybtn.setImageResource(R.drawable.everynotice_daybtn5);
                everynotice_daymic.setVisibility(View.INVISIBLE);
                break;
        }
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    mMediaPlayer.setDataSource(Eaudio);
                    mMediaPlayer.prepare();
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
            }
        }.start();
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                isComplete = true;
            }
        });
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                everynotice_daymic.setImageResource(R.drawable.everynotice_daymic4);
                try {
                    mp.stop();
                    mp.reset();
                    mp.setDataSource(Eaudio);
                    mp.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
                return;
            }
        });
    }


    @Override
    public void onBackPressed() {

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.everynotice_dayclose:
//            case R.id.dialog_mynotice:
                dialog_mynotice.setClickable(false);
                Animation translateAnimationf = new TranslateAnimation(0, 0, 0, UIHelper.dip2px(EveryDayNoticeDialogActivity.this, 600));
                translateAnimationf.setDuration(600);
                rl_mynotice.startAnimation(translateAnimationf);
                translateAnimationf.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        rl_mynotice.setVisibility(View.GONE);
                        finish();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                break;
            case R.id.everynotice_daybtn:
                startActivity(new Intent(this, EveryDayNoticeEventActivity.class));
                break;
            case R.id.everynotice_daymic:
                if (!isComplete) {
                    Toast.makeText(EveryDayNoticeDialogActivity.this, "加载中..", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mMediaPlayer == null)
                    return;
                if (mMediaPlayer.isPlaying()) {
                    everynotice_daymic.setImageResource(R.drawable.everynotice_daymic4);
                    mMediaPlayer.stop();
                    try {
                        mMediaPlayer.prepare();//stop后下次重新播放要首先进入prepared状态
                        mMediaPlayer.seekTo(0);//须将播放时间设置到0；这样才能在下次播放是重新开始，否则会继续上次播放
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    everynotice_daymic.setImageResource(R.drawable.everynotice_daymic4_over);
                    mMediaPlayer.start();
                }
                break;
        }
    }


    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

}
